﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using danreng.Enum;
using MongoDB.Bson;
using sharp_net.Repositories;
using sharp_net.Repositories.MongoDB;

namespace danreng.Domain {
    public class Library : DomainMongo, IAggregateRoot {
        [Required]
        [MaxLength(200)]
        public string Title { get; set; }
        [Required]
        public string Url { get; set; }
        [Required]
        public ObjectId ShopId { get; set; }
        public string EtaoUrl { get; set; }
        public string HuihuiUrl { get; set; }

        public int TypeEnum { get; set; }
        public eLibraryType Host {
            get { return (eLibraryType)TypeEnum; }
            set { TypeEnum = (int)value; }
        }

        [Required]
        public decimal PriceNow { get; set; }
        [Required]
        public string PriceHistoryJson { get; set; }
        public DateTime LastUpdateTime { get; set; }

        public string BuyUrl { get; set; }

        public void Update(LibraryData data) {
            base.Update((DomainMongoData)data);
            if (!string.IsNullOrEmpty(data.Title))
                this.Title = data.Title;
            if (!string.IsNullOrEmpty(data.Url))
                this.Url = data.Url;
            if (!string.IsNullOrEmpty(data.EtaoUrl))
                this.EtaoUrl = data.EtaoUrl;
            if (!string.IsNullOrEmpty(data.HuihuiUrl))
                this.HuihuiUrl = data.HuihuiUrl;
            if (data.TypeEnum != 0)
                this.TypeEnum = data.TypeEnum;
            if (data.PriceNow != 0)
                this.PriceNow = data.PriceNow;
            if (!string.IsNullOrEmpty(data.PriceHistoryJson))
                this.PriceHistoryJson = data.PriceHistoryJson;
            if (data.LastUpdateTime != null)
                this.LastUpdateTime = data.LastUpdateTime;
            if (!string.IsNullOrEmpty(data.BuyUrl))
                this.BuyUrl = data.BuyUrl;
        }
    }

    public class LibraryData : DomainMongoData {
        public string Title { get; set; }
        public string Url { get; set; }
        public string ShopId { get; set; }
        public string EtaoUrl { get; set; }
        public string HuihuiUrl { get; set; }
        public int TypeEnum { get; set; }
        public decimal PriceNow { get; set; }
        public string PriceHistoryJson { get; set; }
        public DateTime LastUpdateTime { get; set; }
        public string BuyUrl { get; set; }
    }
}
